package com.example.maple.ccsclient.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.interfaces.IReportView;
import com.example.maple.ccsclient.presenter.ReportPresenter;

public class ReportActivity extends Activity implements IReportView {

	RelativeLayout load = null;

	WebView mWebView = null;

	ReportPresenter mReportPresenter = null;
	private Animation anim;
	private ImageButton mImageButton;

	String info ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report);

		mReportPresenter = new ReportPresenter(this,this);
		findView();
	}

	private void findView() {

		Intent intent = getIntent();
		info = intent.getStringExtra("info");
		mImageButton = (ImageButton) findViewById(R.id.refresh);
		anim = AnimationUtils.loadAnimation(ReportActivity.this, R.anim.refresh_rotate);
		mImageButton.startAnimation(anim);
		mImageButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//刷新数据
				mReportPresenter.refresh(info);
			}
		});
		mWebView = (WebView) findViewById(R.id.webView);
		load = (RelativeLayout) findViewById(R.id.load);


		//加载数据
		mReportPresenter.refresh(info);
	}


	//显示网页
	@Override
	public void showResult(String result) {

		mWebView.loadDataWithBaseURL(null, result, "text/html", "utf-8", null);
		mWebView.setVisibility(View.VISIBLE);

	}

	@Override
	public void showLoad() {
		mWebView.setVisibility(View.GONE);
		load.setVisibility(View.VISIBLE);
		if (null != anim) {
			anim.start();
		}
	}

	@Override
	public void hiddenLoad() {
		load.setVisibility(View.GONE);
		if (null != anim) {
			anim.cancel();
		}
	}

	@Override
	public void showDialog(String toastText) {
		Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
	}

	public static void startAction(Context context, String par) {
		Intent intent = new Intent(context,ReportActivity.class);
		intent.putExtra("info",par);
		context.startActivity(intent);
	}
}
